<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>观众</title>
</head>
<body>
<video id="video" width="500px" height="500px" autoplay="autoplay"></video>
<script>
    var video=document.getElementById('video')
    var ws = null;
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        connectWebSocket()
    } else {
        alert('Not support websocket')
    }

    function connectWebSocket() {
        ws = new WebSocket("ws://localhost:8081/ws");
    }

    //连接发生错误的回调方法
    ws.onerror = function () {
    };

    //连接成功建立的回调方法
    ws.onopen = function (event) {
        console.log('websocket 连接成功！')
    }

    //接收到消息的回调方法
    ws.onmessage = function (event) {
        var videoData = window.URL.createObjectURL(event.data);
        video.src=videoData
        video.play()// 播放传过来的视频
    }

    //连接关闭的回调方法
    ws.onclose = function () {
        console.log('websocket连接关闭！')
    }
</script>
</body>
</html>